package com.lxd.client;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lxd.entity.ChatMsg;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class ClientHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        System.out.println("ClientHandler Active");
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        try {
            ChatMsg chatMsg = JSON.parseObject(msg.toString(), ChatMsg.class);
            System.out.println("收到消息:" + JSONObject.toJSONString(chatMsg));
            if (null!=chatMsg.getCode()&&0 == chatMsg.getMsgType()) {
                System.out.println("退登：" + chatMsg.getName());
                ctx.channel().close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        super.userEventTriggered(ctx, evt);
    }
}
